﻿#include "DarkDevil.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/ServerPackets.h"
#include "../DelayedAction.h"
#include "../../../Shared/Data/Stat.h"
#include "../MapObject.h"
#include "../../../Shared/Enums.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    DarkDevil::DarkDevil(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool DarkDevil::InAttackRange()
    {
        if (getTarget()->getCurrentMap() != getCurrentMap())
        {
            return false;
        }
        if (getTarget()->getCurrentLocation() == getCurrentLocation())
        {
            return false;
        }

        return Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), getEnvir()->getTime() > _areaTime ? 3 : 1);
    }

    void DarkDevil::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        if (getEnvir()->getTime() < _areaTime)
        {
            MonsterObject::Attack();
            return;
        }

        _areaTime = getEnvir()->getTime() + 2000 + getEnvir()->Random->Next(3) * 1000;

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        S::ObjectRangeAttack *tempVar = new S::ObjectRangeAttack();
        tempVar->ObjectID = ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = getCurrentLocation();
        Broadcast(tempVar);
        DelayedAction tempVar2(DelayedType::RangeDamage, getEnvir()->getTime() + 500);
        ActionList.push_back(&tempVar2);

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;
        ShockTime = 0;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }

    void DarkDevil::CompleteRangeAttack(std::vector<std::any> &data)
    {
        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]) * 3;
        if (damage == 0)
        {
            return;
        }

        std::vector<MapObject*> targets = FindAllTargets(1, Functions::PointMove(getCurrentLocation(), getDirection(), 2));
        if (targets.empty())
        {
            return;
        }

        for (int i = 0; i < targets.size(); i++)
        {
            targets[i]->Attacked(this, damage, DefenceType::MACAgility);
        }
    }
}
